package cn.vconinfo.database.mybatisplus;

import cn.vconinfo.database.inner.config.MybatisConfiguration;
import cn.vconinfo.database.outer.source.DefaultDataSourceConfig;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Slf4j
@Component
@ConditionalOnMissingBean(value = {DefaultDataSourceConfig.class, MybatisConfiguration.class})
public class MybatisPlusConfig {

    /**
     * 逻辑删除字段
     */
    public static final String LOGIC_DELETE_FILED = "DEL_FLAG";

    @Autowired
    private DataSource dataSource;

    @Autowired
    private MybatisPlusProperties properties;

    /**
     * 逻辑删除插件
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setSchema("mysql");
        dbConfig.setLogicDeleteField(LOGIC_DELETE_FILED);
        dbConfig.setLogicDeleteValue("1");
        dbConfig.setLogicNotDeleteValue("0");

        return globalConfig;
    }

    @Primary
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Autowired GlobalConfig globalConfig) throws Exception {
        log.info("初始化SqlSessionFactory");

        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource); //数据源
        sqlSessionFactory.setMapperLocations(
                new PathMatchingResourcePatternResolver()
                        .getResources("classpath*:mapper/**/*.xml")
        );
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //sqlSessionFactory.setMapperLocations(this.properties.resolveMapperLocations());
        sqlSessionFactory.setConfigLocation(resolver.getResource(this.properties.getConfigLocation()));
        sqlSessionFactory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        sqlSessionFactory.setGlobalConfig(globalConfig);
        //sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()});
        //开启驼峰命名转换
        sqlSessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        //开启call-setters-on-nulls属性
        sqlSessionFactory.getObject().getConfiguration().setCallSettersOnNulls(true);
        //当开启这个设置时,返回行的所有列都是空时,MyBatis会返回一个空实例
        sqlSessionFactory.getObject().getConfiguration().setReturnInstanceForEmptyRow(true);
        return sqlSessionFactory.getObject();
    }

}
